package co.fitstart.weapp.api.web.controller.coach;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.result.HttpResult;
import co.fitstart.entity.coach.CoachStudentRelation;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.principal.PrincipalMessage;
import co.fitstart.entity.user.Student;
import co.fitstart.weapp.api.service.CoachCourseService;
import co.fitstart.weapp.api.service.CoachMessageService;
import co.fitstart.weapp.api.service.CoachService;
import co.fitstart.weapp.api.service.CourseRoundService;
import co.fitstart.weapp.api.service.StudentService;

/**
 * @ClassName: CoachProfileController
 * @author cuteHU
 * @date 2018年11月27日
 */
@RestController
@RequestMapping("/coach/profile")
public class CoachProfileController {

    private static final Logger logger = LoggerFactory.getLogger(CoachProfileController.class);

    @Autowired
    private CoachService coachService;

    @Autowired
    private CoachMessageService coachMeaasgeService;

    @Autowired
    private CoachCourseService coachCourseService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private CourseRoundService courseRoundService;

    /**
     * 进入教练个人中心
     * 
     * @return HttpResult 返回类型
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "mobile")
    public HttpResult profile(@RequestParam("mobile") String mobile) {
        HttpResult httpResult = new HttpResult();
        //List<Boolean> isReads = new ArrayList<Boolean>();
        try {
            Principal coach = coachService.load(mobile);
//            List<PrincipalMessage> pMessages = coachMeaasgeService.list(coach);
//            for (PrincipalMessage pMessage : pMessages) {
//                if (pMessage.isRead() == false) {
//                    isReads.add(pMessage.isRead());
//                }
//            }
            Boolean read=false;
            int size=coachService.count(coach, read);
            httpResult.success().message("获取数据成功").data("coach", coach).data("size", size)
            .data("profile", coach.getProfile());
        } catch (Exception e) {
            logger.error("#CoachProfileController# list() error", e);
            httpResult.fail("500").message("获取数据失败");
        }
        return httpResult;
    }

    /**
     * 进入教练中我的消息
     * 
     * @return HttpResult 返回类型
     */
    @RequestMapping(value = "/messages", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "mobile")
    public HttpResult listMessages(@RequestParam("mobile") String mobile,
                                   @RequestParam(value = "begin", required = false, defaultValue = "20") int begin,
                                   @RequestParam(value = "length", required = false, defaultValue = "20") int length) {
        HttpResult httpResult = new HttpResult();
        Principal coach = coachService.load(mobile);
        try {
            List<PrincipalMessage> principalMessages = coachMeaasgeService.list(coach, begin, length);
            httpResult.success().message("获取数据成功").data("principalMessages", principalMessages);
        } catch (Exception e) {
            logger.error("#CoachProfileController# list() error", e);
            httpResult.fail("500").message("获取数据失败");
        }
        return httpResult;
    }

    /**
     * @Description: 修改教练消息
     * @param: messageId isRead
     * @return: HttpResult
     */

    @RequestMapping(value = "/messages/{messageId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "mobile")
    public HttpResult readMessage(@PathVariable("messageId") Long messageId, @RequestParam("mobile") String mobile) {
        HttpResult httpResult = new HttpResult();
        try {
            coachMeaasgeService.update(messageId);
            httpResult.success().message("该消息已阅读");
        } catch (Exception e) {
            logger.error("#CoachProfileController#", e);
            httpResult.fail("500").message("修改教练消息失败");
        }
        return httpResult;
    }

    /**
     * 进入教练中我的课程 (默认为1v1的课程)
     * 
     * @param courseType
     * @return HttpResult 返回类型
     */
    @RequestMapping(value = "/coach-students", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "mobile")
    public HttpResult listCoachStudentRelations(@RequestParam("mobile") String mobile,
            @RequestParam(name = "courseType", required = true, defaultValue = "individual") String courseType) {
        HttpResult httpResult = new HttpResult();
        Principal principal = coachService.load(mobile);
        try {
            List<CoachStudentRelation> coachStudentRelations = coachCourseService.list(principal, courseType);
            
            httpResult.success().message("获取数据成功").data("coachStudentRelations", coachStudentRelations);
        } catch (Exception e) {
            logger.error("#CoachProfileController#", e);
            httpResult.fail("500").message("获取数据失败");
        }
        return httpResult;
    }

    /**
     * 进入教练中我的课程(单个课程详情页)
     * 
     * @param scheduleId
     * @return HttpResult 返回类型
     */
    @RequestMapping(value = "/coach-students/{studentId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "mobile")
    public HttpResult listStudentCourse(@PathVariable("studentId") Long studentId) {
        HttpResult httpResult = new HttpResult();
        try {
            Student student = studentService.load(studentId);
            
            List<CourseRound> courseRounds = courseRoundService.list(student);
            
            httpResult.success().message("获取数据成功")
                                .data("studentAvatar", student.getUser().getProfile() == null ? null : student.getUser().getProfile().getAvatar())
                                .data("studentName", student.getName())
                                .data("studentMobile", student.getMobile())
                                .data("courseRounds", courseRounds);
        } catch (Exception e) {
            logger.error("#CoachProfileController# listStudentCourse() error", e);
            httpResult.fail("500").message("获取数据失败");
        }
        return httpResult;
    }
}
